package com.yunhe.rentcars.pro.controller.permi;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.commons.dto.TreeNode;
import com.yunhe.rentcars.pro.entity.permi.Role;
import com.yunhe.rentcars.pro.service.permi.PermissionService;
import com.yunhe.rentcars.pro.service.permi.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Administrator on 2017/10/23 0023.
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;
    @RequestMapping
    public String list(){
        return "permi/role/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Pageable listData(Integer offset, Integer limit){
        Pageable<Role> pageable = roleService.findPage(offset, limit);
        return  pageable;
    }

    @RequestMapping("/add")
    public String add(){
        return "permi/role/add";
    }

    @RequestMapping("/edit")
    public ModelAndView edit(Long id){

        Role role = roleService.find(id);
        ModelAndView mav = new ModelAndView("permi/role/edit");
        mav.addObject("role",role);
        return mav;
    }

    @RequestMapping("/save")
    public String save(Role role, String permissionIds){
        roleService.save(role,permissionIds);
        return "redirect:/role";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(String ids){
        roleService.deleteBatch(ids);
        return JSONResponse.success("删除成功");
    }

    @RequestMapping("/update")
    public String update(Role role, String permissionIds){
        roleService.update(role,permissionIds);
        return "redirect:/role";
    }

    @RequestMapping("/tree")
    @ResponseBody
    public JSONResponse tree(Long roleid){
        List<TreeNode> treeNodes = permissionService.findTreeNodes(roleid);
        return JSONResponse.success("权限树节点获取成功",treeNodes);
    }
}
